package com.woniu.repaircenter.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.exception.XinanException;
import com.woniu.exception.repaircenter.RepaircenterException;
import com.woniu.repaircenter.models.dto.HousingMaterialDTO;
import com.woniu.repaircenter.models.param.SearchMaterialParam;
import com.woniu.repaircenter.models.param.UpdateMaterialParam;
import com.woniu.repaircenter.service.mapper.HousingMaterialMapper;
import com.woniu.repaircenter.service.model.HousingMaterial;
import com.woniu.repaircenter.service.service.HousingMaterialService;
import com.woniu.utils.EnumException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 材料表 服务实现类
 * </p>
 *
 * @author 吕雨泽
 * @since 2024年12月06日
 */
@Service
public class HousingMaterialServiceImpl extends ServiceImpl<HousingMaterialMapper, HousingMaterial> implements HousingMaterialService {

    @Autowired
    private HousingMaterialMapper housingMaterialMapper;

    /**
     * 新增材料
     *
     * @param housingMaterial 材料对象
     * @throws XinanException 如果新增过程中发生异常
     */
    @Override
    public void insertHousingMaterial(HousingMaterial housingMaterial) throws XinanException {
        housingMaterial.setState(0); // 设置默认状态为0（可用）
        try {
            housingMaterialMapper.insert(housingMaterial);
        } catch (DuplicateKeyException e) {
            throw new RepaircenterException(EnumException.REPAIR_MATERIAL_EXIST);
        }
    }

    /**
     * 删除材料
     *
     * @param housingMaterialCode 材料编号
     * @throws XinanException 如果删除过程中发生异常
     */
    @Override
    public void removeMaterial(String housingMaterialCode) throws XinanException {
        Integer count = housingMaterialMapper.removeMaterial(housingMaterialCode);
        if (count == 0) {
            throw new RepaircenterException(EnumException.REPAIR_MATERIAL_NOT_EXIST);
        }
    }

    /**
     * 查询材料列表
     *
     * @param searchMaterialParam 查询条件
     * @return 包含材料列表的结果对象
     * @throws XinanException 如果查询过程中发生异常
     */
    @Override
    public PageInfo<HousingMaterialDTO> searchHousingMaterialList(SearchMaterialParam searchMaterialParam) throws XinanException {
        PageHelper.startPage(searchMaterialParam.getPageNum(), searchMaterialParam.getPageSize());
        List<HousingMaterialDTO> housingMaterialDTOS = housingMaterialMapper.searchHousingMaterialList(searchMaterialParam);
        return new PageInfo<>(housingMaterialDTOS);
    }

    /**
     * 更新材料信息
     *
     * @param param 更新参数
     * @throws XinanException 如果更新过程中发生异常
     */
    @Override
    public void updateHousingMaterial(UpdateMaterialParam param) throws XinanException {
        Integer count = housingMaterialMapper.updateHousingMaterial(param);
        if (count == 0) {
            throw new RepaircenterException(EnumException.REPAIR_MATERIAL_NOT_EXIST);
        }
    }

    /**
     * 根据材料编号获取材料状态
     *
     * @param materialCode 材料编号
     * @return 材料对象
     * @throws XinanException 如果查询过程中发生异常
     */
    @Override
    public HousingMaterial seatateHousingMaterial(String materialCode) throws XinanException {
        if (materialCode == null) {
            throw new RepaircenterException(EnumException.REPAIR_MATERIAL_NOT_EXIST);
        }
        HousingMaterial housingMaterial = housingMaterialMapper.seatateHousingMaterial(materialCode);
        if (housingMaterial == null) {
            throw new RepaircenterException(EnumException.REPAIR_MATERIAL_NOT_EXIST);
        }
        return housingMaterial;
    }

    /**
     * 更新材料库存
     *
     * @param materialCode 材料编号
     * @param count 库存数量
     * @return 更新的记录数
     */
    @Override
    public Integer updateHousingInventory(String materialCode, Integer count) throws XinanException{
        try {
            Integer updatedCount = housingMaterialMapper.updateHousingInventory(materialCode, count);
            return updatedCount;
        } catch (Exception e) {
            throw new RepaircenterException(EnumException.REPAIR_MATERIAL_NOT_EXIST);
        }
    }

    @Override
    public List<HousingMaterialDTO> getHousingMaterialList() {
        QueryWrapper<HousingMaterial> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", 0);
        List<HousingMaterial> housingMaterials = housingMaterialMapper.selectList(queryWrapper);
        List<HousingMaterialDTO> housingMaterialDTOS = BeanUtil.copyToList(housingMaterials, HousingMaterialDTO.class);
        return housingMaterialDTOS;
    }

    @Override
    public void updateHousing(String materialCode, Integer count) {

            Integer updatedCount = housingMaterialMapper.updateHousing(materialCode, count);

    }

    /**
     * 恢复材料
     *
     * @param housingMaterialCode 材料编号
     * @throws XinanException 如果恢复过程中发生异常
     */
    @Override
    public void recoverMaterial(String housingMaterialCode) throws XinanException {
        Integer count = housingMaterialMapper.recoverMaterial(housingMaterialCode);
        if (count == 0) {
            throw new RepaircenterException(EnumException.REPAIR_MATERIAL_NOT_EXIST);
        }
    }
}
